<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item>
          <el-input v-model="listPram.roleName" :placeholder="$t('qingShuRuJiaoSeNiCheng')" clearable class="selWidth" />
        </el-form-item>
        <el-form-item>
          <el-button size="mini" type="primary" @click.native="handleGetRoleList">{{ $t('chaXun') }}</el-button>
        </el-form-item>
      </el-form>
      <el-form inline @submit.native.prevent>
        <el-form-item>
          <el-button
            v-hasPermi="['merchant:admin:role:save']"
            size="small"
            type="primary"
            @click="handlerOpenEdit(0)"
          >{{ $t('tianJiaJiaoSe') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="listData.list"
        size="mini"
        :header-cell-style="{ fontWeight: 'bold', background: '#f8f8f9', color: '#515a6e', height: '40px' }"
      >
        <el-table-column :label="$t('jiaoSeBianHao')" prop="id" width="120" />
        <el-table-column :label="$t('jiaoSeNiCheng')" prop="roleName" min-width="130" />
        <el-table-column :label="$t('zhuangTai')" prop="status">
          <template v-if="checkPermi(['merchant:admin:role:update:status'])" slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="true"
              :inactive-value="false"
              style="width: 40px"
              @change="handleStatusChange(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column :label="$t('chuangJianShiJian')" prop="createTime" min-width="150" />
        <el-table-column :label="$t('gengXinShiJian')" prop="updateTime" min-width="150" />
        <el-table-column :label="$t('caoZuo')" min-width="130" fixed="right">
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['merchant:admin:role:info']"
              size="small"
              type="text"
              @click="handlerOpenEdit(1, scope.row)"
            >{{ $t('bianJi') }}</el-button>
            <el-button
              v-hasPermi="['merchant:admin:role:delete']"
              size="small"
              type="text"
              @click="handlerOpenDel(scope.row)"
            >{{ $t('shanchu') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="listPram.page"
        :page-sizes="constants.page.limit"
        :layout="constants.page.layout"
        :total="listData.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
      :visible.sync="editDialogConfig.visible"
      :title="editDialogConfig.isCreate === 0 ? $t('chuangJianShenFen') : $t('bianJiShenFen')"
      destroy-on-close
      :close-on-click-modal="false"
      width="500px"
    >
      <edit
        v-if="editDialogConfig.visible"
        ref="editForm"
        :is-create="editDialogConfig.isCreate"
        :edit-data="editDialogConfig.editData"
        @hideEditDialog="hideEditDialog"
      />
    </el-dialog>
  </div>
</template>

<script>
import * as roleApi from '@/api/role.js'
import edit from './edit'
import { checkPermi } from '@/utils/permission' // 权限判断函数
export default {
  // name: "index"
  components: { edit },
  data() {
    return {
      constants: this.$constants,
      listData: { list: [] },
      listPram: {
        createTime: null,
        updateTime: null,
        level: null,
        page: 1,
        limit: this.$constants.page.limit[0],
        roleName: '',
        rules: null,
        status: ''
      },
      menuList: [],
      editDialogConfig: {
        visible: false,
        isCreate: 0, // 0=创建，1=编辑
        editData: {}
      }
    }
  },
  mounted() {
    this.handleGetRoleList()
  },
  methods: {
    checkPermi,
    handlerOpenDel(rowData) {
      this.$modalSure(this.$t('shanChuDangQianShuJu')).then(() => {
        roleApi.delRole(rowData).then((data) => {
          this.$message.success(this.$t('shanChuChengGong'))
          this.handleGetRoleList()
        })
      })
    },
    handleGetRoleList() {
      roleApi.getRoleList(this.listPram).then((data) => {
        this.listData = data
      })
    },
    handlerOpenEdit(isCreate, editDate) {
      isCreate === 1 ? (this.editDialogConfig.editData = editDate) : (this.editDialogConfig.editData = {})
      this.editDialogConfig.isCreate = isCreate
      this.editDialogConfig.visible = true
    },
    hideEditDialog() {
      this.editDialogConfig.visible = false
      this.handleGetRoleList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.handleGetRoleList(this.listPram)
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.handleGetRoleList(this.listPram)
    },
    // 修改状态
    handleStatusChange(row) {
      roleApi.updateRoleStatus(row).then((res) => {
        this.$message.success(this.$t('gengXinChengGong'))
        this.handleGetRoleList()
      })
    }
  }
}
</script>

<style scoped lang="scss"></style>
